package org.gdstash.character;

import org.gdstash.file.GDReader;
import org.gdstash.file.GDWriter;
import org.gdstash.util.FileVersionException;
import org.gdstash.util.GDMsgFormatter;

import java.io.IOException;

public class GDCharHeader {
    private static final int VERSION_1 = 1;
    private static final int VERSION_2 = 2;
    private int version;
    private String charName;
    private byte sex;
    private String classID;
    private int level;
    private byte hardcore;
    private byte expansionStatus;
    private boolean changed;

    public String getCharName() {
        return this.charName;
    }

    public byte getSex() {
        return this.sex;
    }

    public String getClassID() {
        return this.classID;
    }

    public int getLevel() {
        return this.level;
    }

    public boolean isHardcore() {
        return this.hardcore == 1;
    }

    public boolean isAsheshOfMalmouthChar() {
        return this.expansionStatus == 1;
    }

    public boolean isForgottenGodsChar() {
        return this.expansionStatus == 3;
    }

    public boolean isFangsOfAsterkarnChar() {
        return this.expansionStatus == 7;
    }

    public boolean hasChanged() {
        return this.changed;
    }

    public void setCharName(final String charName) {
        this.charName = charName;
        this.changed = true;
    }

    public void setSex(final byte sex) {
        if (sex == 0) {
            this.sex = 0;
        } else {
            this.sex = 1;
        }
        this.changed = true;
    }

    public void setLevel(final int level) {
        this.level = level;
        this.changed = true;
    }

    public void setHardcore(final boolean hardcore) {
        if (hardcore) {
            this.hardcore = 1;
        } else {
            this.hardcore = 0;
        }
        this.changed = true;
    }

    public void read() throws IOException {
        this.version = GDReader.readEncInt(true);
        if (this.version != 1 && this.version != 2) {
            throw new FileVersionException(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_UNSUPPORTED_VERSION"));
        }
        this.charName = GDReader.readEncWideString();
        this.sex = GDReader.readEncByte();
        this.classID = GDReader.readEncString();
        this.level = GDReader.readEncInt(true);
        this.hardcore = GDReader.readEncByte();
        if (this.version >= 2) {
            this.expansionStatus = GDReader.readEncByte();
        }
        this.changed = false;
    }

    public void write() throws IOException {
        GDWriter.writeInt(this.version);
        GDWriter.writeWideString(this.charName);
        GDWriter.writeByte(this.sex);
        GDWriter.writeString(this.classID);
        GDWriter.writeInt(this.level);
        GDWriter.writeByte(this.hardcore);
        if (this.version >= 2) {
            GDWriter.writeByte(this.expansionStatus);
        }
        this.changed = false;
    }

    public int getByteSize() {
        int size = 0;
        size += 4;
        size += 4;
        if (this.charName != null) {
            size += this.charName.length() * 2;
        }
        ++size;
        size += 4;
        if (this.classID != null) {
            size += this.classID.length();
        }
        size += 4;
        ++size;
        if (this.version >= 2) {
            ++size;
        }
        return size;
    }
}
